package f.app.al;

import java.util.List;

import f.app.al.GraphDetails;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.BitmapFactory.Options;
import android.util.AttributeSet;
import android.view.View;

public class ViewGraph extends View {
 
 public static final int WAIT = 0;
 public static final int IS_READY_TO_DRAW = 1;
 public static final int IS_DRAW = 2;
 private static final float START_INC = 30;
 private Paint mBgPaints = new Paint();
 private Paint mLinePaints = new Paint();
 
 private int mOverlayId;
 private int mWidth;
 private int mHeight;
 private int mGapTop;
 private int mGapBottom;
 private int mBgColor;
 private int mGapLeft;
 private int mGapRight;
 private int mState = WAIT;
 private float mStart;
 private float mSweep;
 private int mMaxConnection;
 private List<GraphDetails> mDataArray;
 

	public ViewGraph (Context context){
		super(context);
	}
	 
	public ViewGraph(Context context, AttributeSet attrs) {
	    super(context, attrs);
	}
	 
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (mState != IS_READY_TO_DRAW) {
			return;
		}
		canvas.drawColor(mBgColor);
	
		mBgPaints.setAntiAlias(true);
		mBgPaints.setStyle(Paint.Style.FILL);
		mBgPaints.setColor(0x88FF0000);
		mBgPaints.setStrokeWidth(0.5f);
		
		mLinePaints.setAntiAlias(true);
		mLinePaints.setStyle(Paint.Style.STROKE);
		mLinePaints.setColor(0xff000000);
		mLinePaints.setStrokeWidth(0.5f);
		
		RectF mOvals = new RectF( mGapLeft, mGapTop, mWidth - mGapRight, mHeight - mGapBottom);
		
		mStart = START_INC;
		GraphDetails item;
		for (int i = 0; i < mDataArray.size(); i++) {
			item = (GraphDetails) mDataArray.get(i);
			mBgPaints.setColor(item.color);
			mSweep = (float) 360* ((float) item.count / (float) mMaxConnection);
			canvas.drawArc(mOvals, mStart, mSweep, true, mBgPaints);
			canvas.drawArc(mOvals, mStart, mSweep, true, mLinePaints);
			mStart = mStart + mSweep;
		}   	
		
		Options options = new BitmapFactory.Options();
        options.inScaled = false;
    	Bitmap OverlayBitmap = BitmapFactory.decodeResource(getResources(), mOverlayId, options);
    	canvas.drawBitmap(OverlayBitmap, 0.0f, 0.0f, null);
    	
		mState = IS_DRAW;
	}
	
	public void setGeometry(int width, int height, int GapLeft, int GapRight, int GapTop, int GapBottom, int OverlayId) {
    	mWidth     = width;
   	 	mHeight    = height;
   	 	mGapLeft   = GapLeft;
   	 	mGapRight  = GapRight;
   	 	mGapTop    = GapTop;
   	 	mGapBottom = GapBottom;
   	 	mOverlayId = OverlayId;
    }
	
	public void setSkinParams(int bgColor) {
   	 	mBgColor   = bgColor;
    }
	
	public void setData(List<GraphDetails> data, int MaxConnection) {
    	mDataArray = data;
    	mMaxConnection = MaxConnection;
    	mState = IS_READY_TO_DRAW;
	}
	
	public void setState(int State) {
	    mState = State;
	}
	   
	public int getColorValue( int Index ) {
	   	 if (mDataArray == null) {
	   		 return 0;
	   	 }
	   	 if (Index < 0){
	   	 	return ((GraphDetails)mDataArray.get(0)).color;
	   	 } else if (Index >= mDataArray.size()){
	   	 	return ((GraphDetails)mDataArray.get(mDataArray.size()-1)).color;
	   	 } else {
	   	 	return ((GraphDetails)mDataArray.get(mDataArray.size()-1)).color;
	   	 }
	}
}